package itcast.shops.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import itcast.dao.shopsDao;
import itcast.domain.CarBean;
import itcast.domain.User;
import itcast.service.ShopsService;
import itcast.service.ShopsServiceimpl;

/**
 * 转发
 */
@WebServlet("/settlementServlet")
public class SettlementServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SettlementServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1、设置编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//2、接收从购物车结算的商品id，返显订单生成页面！
		String [] shopids=request.getParameterValues("shopid");
		//3、获取用户id
		User user=(User) request.getSession().getAttribute("user");
		int i=user.getUserid();
		//4、调用service把shopid和用户id相对应的都读出来装在这个集合里面
		ShopsService service=new ShopsServiceimpl();
		List<CarBean> listcarBean=service.findcarsshops(shopids,i);
		//5、计算商品总价格商品总件数
		   float totalprices=0;//商品总价格
			int sum=0;//商品总件数
			for (CarBean carBean : listcarBean) {
				sum+=carBean.getCounts();
				totalprices+=carBean.getPrices()*carBean.getCounts();
			}
			
		//6、存储信息
		request.setAttribute("totalsum",sum);//商品总件数
		request.setAttribute("totalprices",totalprices);//商品总价格
		request.setAttribute("listcarBean",listcarBean);
		
		//7、转发到订单提交页面
		request.getRequestDispatcher("settlementshow.jsp").forward(request, response);
	}

}
